##%%%%%%%%%%%%%%%%%%%%%%%%%% InOutRsq.R %%%%%%%%%%%%%%%%%%%%%%%%%%%%
##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

## In this script, we compute the intermediate statistics  
## (r.squared, fstat, tstat, etc.)using the data generated 
## by the script "SIMN"

library(Matrix)
library(MASS) ## For ginv() - Moore penrose inverse

##%%%%%%%% Set Working Directory %%%%%%%%%%%%
WorkDir="D:/RWORK/Repository Files"
setwd(WorkDir)

##%%%%%%%%%% Read in function files %%%%%%%%%

source("FUNC.R")

##%%%%%% Load simulation data from disk %%%%%%%

load("insL2");load("t1L2");load("t2L2")
load("insL3");load("t1L3");load("t2L3")
load("insL4");load("t1L4");load("t2L4")
load("insL4S");load("t1L4S");load("t2L4S")
load("insL7");load("t1L7");load("t2L7")
load("insL7S");load("t1L7S");load("t2L7S")

##%%%%%% Create index matrices %%%%%%%%%%%%%
##%%%%%%%%% In sample indexes %%%%%%%%
## for N=5000
N=5000
II = 1:5000
Ivec=(0:199)*5000
Imat5=matrix(0,5000,200)
for(i in 1:200){ Imat5[,i]=II+Ivec[i]}

## for N=2500
N=2500
II = 1:2500
Ivec=(0:399)*2500
Imat2=matrix(0,2500,400)
for(i in 1:400){ Imat2[,i]=II+Ivec[i]}

## for N=1125
Nfe=1125
IIfe = 1:1125
Ivecfe=(0:799)*1125
Imat1=matrix(0,1125,800)
for(i in 1:800){ Imat1[,i]=IIfe+Ivecfe[i]}

## for N=500
N=500
IvecS=(0:1999)*500
IIS = 1:500
Imat.5=matrix(0,500,2000)
for(i in 1:2000){ Imat.5[,i]=IIS+IvecS[i]}


##%%%%%%%%%%%%%%%%%%% Out of sample indexes %%%%%%%%%%%%%%%%%%
## Out samples using the next index set in the matrix
## except the last one which goes back to 1 
Imat5Out=Imat5[,c(2:200,1)]
Imat2Out=Imat2[,c(2:400,1)]
Imat1Out=Imat1[,c(2:800,1)]
Imat.5Out=Imat.5[,c(2:2000,1)]

##%%%%%%%%%% Create long (50K) out of sample sets %%%%%%%%%%%
N=50000

Imat5.50Out=matrix(0,50000,200)
Imat2.50Out=matrix(0,50000,200)
Imat1.50Out=matrix(0,50000,200)
Imat.5.50Out=matrix(0,50000,200)

for(i in 1:200){
	ii50=setdiff(sample(1:1000000,55000,replace=F),Imat5[,i])[1:50000]
 	Imat5.50Out[,i]=ii50
	ii50=setdiff(sample(1:1000000,52500,replace=F),Imat2[,i])[1:50000]
 	Imat2.50Out[,i]=ii50
	ii50=setdiff(sample(1:1000000,51125,replace=F),Imat1[,i])[1:50000]
 	Imat1.50Out[,i]=ii50
	ii50=setdiff(sample(1:1000000,50500,replace=F),Imat.5[,i])[1:50000]
 	Imat.5.50Out[,i]=ii50
	}


##%%%%%%%%%%%%%%%%%%%%%% Save these %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save(Imat5,file="Imat5")
save(Imat2,file="Imat2")
save(Imat1,file="Imat1")
save(Imat.5,file="Imat.5")

save(Imat5Out,file="Imat5Out")
save(Imat2Out,file="Imat2Out")
save(Imat1Out,file="Imat1Out")
save(Imat.5Out,file="Imat.5Out")


save(Imat5.50Out,file="Imat5.50Out")
save(Imat2.50Out,file="Imat2.50Out")
save(Imat1.50Out,file="Imat1.50Out")
save(Imat.5.50Out,file="Imat.5.50Out")



##%%%%%%%%%%%%%% Make rsq Matrices and Array's %%%%%%%%%%%%%%%%%%%%
##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

##%%%%%%%%%%% Do computations %%%%%%%%%%%%%%%%%%%%%%
##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
##%%%%%%%%%%% Just the longest 5K data sets %%%%%%%%%%%%%%
## This will include L2, L3, L4, L4S, L7S
## L7 N=5000 about 10h-11h each for these
rsqA7t1N5=makeRsqMatNcvAL(t1L7,insL7,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA7t1N5,file="rsqA7t1N5")

rsqA7t2N5=makeRsqMatNcvAL(t2L7,insL7,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA7t2N5,file="rsqA7t2N5")


## L7S N=5000
rsqA7St1N5=makeRsqMatNcvAL(t1L7S,insL7S,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA7St1N5,file="rsqA7St1N5") # Run 10PM 1-22

rsqA7St2N5=makeRsqMatNcvAL(t2L7S,insL7S,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA7St2N5,file="rsqA7St2N5") # Run 10PM 1-22


## L4S N=5000
rsqA4St1N5=makeRsqMatNcvAL(t1L4S,insL4S,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA4St1N5,file="rsqA4St1N5")  # Run 12PM 1-22

rsqA4St2N5=makeRsqMatNcvAL(t2L4S,insL4S,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA4St2N5,file="rsqA4St2N5") # Run 12PM 1-22

## L4 N=5000
rsqA4t1N5=makeRsqMatNcvAL(t1L4,insL4,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA4t1N5,file="rsqA4t1N5") # Run 1AM 1-23

rsqA4t2N5=makeRsqMatNcvAL(t2L4,insL4,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA4t2N5,file="rsqA4t2N5") # Run 1AM 1-23

## L3 N=5000
rsqA3t1N5=makeRsqMatNcvAL(t1L3,insL3,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA3t1N5,file="rsqA3t1N5") # Run 9AM 1-23

rsqA3t2N5=makeRsqMatNcvAL(t2L3,insL3,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA3t2N5,file="rsqA3t2N5") # Run 9AM 1-23

## L2 N=5000
rsqA2t1N5=makeRsqMatNcvAL(t1L2,insL2,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA2t1N5,file="rsqA2t1N5") # Run 9AM 1-23

rsqA2t2N5=makeRsqMatNcvAL(t2L2,insL2,Imat5[,1:200],Imat5Out[,1:200],Imat5.50Out[,1:200])
save(rsqA2t2N5,file="rsqA2t2N5") # Run 9AM 1-23

##%%%%%%%%%%% 2.5K data sets %%%%%%%%%%%%%%
## L7 N=5000 about 10h-11h each for these
rsqA7t1N2=makeRsqMatNcvAL(t1L7,insL7,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA7t1N2,file="rsqA7t1N2")

rsqA7t2N2=makeRsqMatNcvAL(t2L7,insL7,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA7t2N2,file="rsqA7t2N2")

## L7S N=2500
rsqA7St1N2=makeRsqMatNcvAL(t1L7S,insL7S,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA7St1N2,file="rsqA7St1N2")  # Run 10AM 1-23

rsqA7St2N2=makeRsqMatNcvAL(t2L7S,insL7S,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA7St2N2,file="rsqA7St2N2")  # Run 12 noon 1-23

## L4S N=2500
rsqA4St1N2=makeRsqMatNcvAL(t1L4S,insL4S,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA4St1N2,file="rsqA4St1N2") # Run 1PM 1-23

rsqA4St2N2=makeRsqMatNcvAL(t2L4S,insL4S,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA4St2N2,file="rsqA4St2N2") # Run 2PM 1-23


## L4 N=2500
rsqA4t1N2=makeRsqMatNcvAL(t1L4,insL4,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA4t1N2,file="rsqA4t1N2") # Run 2PM 1-23

rsqA4t2N2=makeRsqMatNcvAL(t2L4,insL4,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA4t2N2,file="rsqA4t2N2") # Run 4PM 1-23

## L3 N=2500
rsqA3t1N2=makeRsqMatNcvAL(t1L3,insL3,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA3t1N2,file="rsqA3t1N2") # Run 2PM 1-23

rsqA3t2N2=makeRsqMatNcvAL(t2L3,insL3,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA3t2N2,file="rsqA3t2N2") # Run 3PM 1-23

## L2 N=2500
rsqA2t1N2=makeRsqMatNcvAL(t1L2,insL2,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA2t1N2,file="rsqA2t1N2") # Run 2:30PM 1-23

rsqA2t2N2=makeRsqMatNcvAL(t2L2,insL2,Imat2[,1:200],Imat2Out[,1:200],Imat2.50Out[,1:200])
save(rsqA2t2N2,file="rsqA2t2N2") # Run 4:30PM 1-23



##%%%%%%%%%%% 1.125K data sets %%%%%%%%%%%%%%
## This will include L2, L3, L4, L4S, L7S

## L7 N=1125
rsqA7t1N1=makeRsqMatNcvAL(t1L7,insL7,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA7t1N1,file="rsqA7t1N1")

rsqA7t2N1=makeRsqMatNcvAL(t2L7,insL7,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA7t2N1,file="rsqA7t2N1")


## L7S N=1125
rsqA7St1N1=makeRsqMatNcvAL(t1L7S,insL7S,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA7St1N1,file="rsqA7St1N1") # Run 2:30PM 1-23

rsqA7St2N1=makeRsqMatNcvAL(t2L7S,insL7S,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA7St2N1,file="rsqA7St2N1") # Run 3:30PM 1-23

## L4S N=1125
rsqA4St1N1=makeRsqMatNcvAL(t1L4S,insL4S,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA4St1N1,file="rsqA4St1N1") # Run 3:30PM 1-23

rsqA4St2N1=makeRsqMatNcvAL(t2L4S,insL4S,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA4St2N1,file="rsqA4St2N1") # Run 3:30PM 1-23

## L4 N=1125
rsqA4t1N1=makeRsqMatNcvAL(t1L4,insL4,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA4t1N1,file="rsqA4t1N1") # Run 4:30PM 1-23

rsqA4t2N1=makeRsqMatNcvAL(t2L4,insL4,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA4t2N1,file="rsqA4t2N1") # Run 4:30PM 1-23

## L3 N=1125
rsqA3t1N1=makeRsqMatNcvAL(t1L3,insL3,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA3t1N1,file="rsqA3t1N1")  # Run 4:30PM 1-23

rsqA3t2N1=makeRsqMatNcvAL(t2L3,insL3,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA3t2N1,file="rsqA3t2N1") # Run 4:30PM 1-23

## L2 N=1125
rsqA2t1N1=makeRsqMatNcvAL(t1L2,insL2,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA2t1N1,file="rsqA2t1N1") # Run 5PM 1-23

rsqA2t2N1=makeRsqMatNcvAL(t2L2,insL2,Imat1[,1:200],Imat1Out[,1:200],Imat1.50Out[,1:200])
save(rsqA2t2N1,file="rsqA2t2N1") # Run 5PM 1-23

##%%%%%%%%%%% 0.5K data sets %%%%%%%%%%%%%%
## This will include L2, L3, L4, L4S, L7S

## L7 N=500
rsqA7t1N.5=makeRsqMatNcvAL(t1L7,insL7,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA7t1N.5,file="rsqA7t1N.5")

rsqA7t2N.5=makeRsqMatNcvAL(t2L7,insL7,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA7t2N.5,file="rsqA7t2N.5")



## L7S N=500
rsqA7St1N.5=makeRsqMatNcvAL(t1L7S,insL7S,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA7St1N.5,file="rsqA7St1N.5")

rsqA7St2N.5=makeRsqMatNcvAL(t2L7S,insL7S,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA7St2N.5,file="rsqA7St2N.5") # run

## L4S N=500
rsqA4St1N.5=makeRsqMatNcvAL(t1L4S,insL4S,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA4St1N.5,file="rsqA4St1N.5") # run

rsqA4St2N.5=makeRsqMatNcvAL(t2L4S,insL4S,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA4St2N.5,file="rsqA4St2N.5") # run


## L4 N=500
rsqA4t1N.5=makeRsqMatNcvAL(t1L4,insL4,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA4t1N.5,file="rsqA4t1N.5")

rsqA4t2N.5=makeRsqMatNcvAL(t2L4,insL4,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA4t2N.5,file="rsqA4t2N.5")

## L3 N=500
rsqA3t1N.5=makeRsqMatNcvAL(t1L3,insL3,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA3t1N.5,file="rsqA3t1N.5")

rsqA3t2N.5=makeRsqMatNcvAL(t2L3,insL3,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA3t2N.5,file="rsqA3t2N.5")

## L2 N=500
rsqA2t1N.5=makeRsqMatNcvAL(t1L2,insL2,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA2t1N.5,file="rsqA2t1N.5")

rsqA2t2N.5=makeRsqMatNcvAL(t2L2,insL2,Imat.5[,1:200],Imat.5Out[,1:200],Imat.5.50Out[,1:200])
save(rsqA2t2N.5,file="rsqA2t2N.5")


